package com.taxshare.purchaser.service.rulesReview;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.entity.InvoiceAddressRulesEntity;
import com.taxshare.purchaser.entity.InvoiceCollectionEntity;
import com.taxshare.purchaser.mapper.BlackWhiteMapper;
import com.taxshare.purchaser.service.InvoiceAddressRulesService;
import com.taxshare.purchaser.view.vo.OrganizeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: AdressReviewCheck
 * @Description:
 * @author: hjh
 * @date: 2021/11/16 9:32
 */
@Component
public class AddressReviewCheck implements RuleMainHandler {
    /**
     * 中文，字母,数字正则
     */
    private static String REGEX_RULE = "[a-zA-Z0-9\\u4e00-\\u9fa5]";

    @Autowired
    InvoiceAddressRulesService invoiceAddressRulesService;

    @Autowired
    BlackWhiteMapper blackWhiteMapper;

    @Override
    public CheckInfoRespVO checkRule(InvoiceCollectionEntity bean, String pid) {
        InvoiceAddressRulesEntity info = getInfo(pid);
        CheckInfoRespVO vo = new CheckInfoRespVO();
        if (info != null) {
            OrganizeVO organize = blackWhiteMapper.getOrganize(bean.getPurchaserTaxNo(), bean.getPurchaserName());
            if (organize == null) {
                return vo;
            }
            String address = organize.getAddress() == null ? "" : organize.getAddress();
            String phone = organize.getPhone() == null ? "" : organize.getPhone();
            String bankAddrPhone = organize.getBankAddrPhone() == null ? "" : organize.getBankAddrPhone();
            String bankAccount = organize.getBankAccount() == null ? "" : organize.getBankAccount();
            String addressPhone = getRegexRule(address + phone);
            String bank = getRegexRule(bankAddrPhone + bankAccount);
            String purchaserBank = "";
            String purchaserAddressPhone = "";
            if (CodeConstant.INVOICE_ZYFP.equals(bean.getInvoiceType())) {
                if (StrUtil.isBlank(bean.getPurchaserAddrPhone()) || StrUtil.isBlank(bean.getPurchaserBrank())) {
                    vo.setGoing(false);
                    vo.setFlag(false);
                    if (StrUtil.isNotBlank(info.getAddressNotPassReasons())) {
                        vo.setMsg(info.getAddressNotPassReasons());
                    } else {
                        vo.setMsg("购方银行账户与购方地址电话需都存在！");
                    }
                    vo.setPass(CodeConstant.RULE_1);

                    return vo;
                }
                purchaserBank = getRegexRule(bean.getPurchaserBrank());
                purchaserAddressPhone = getRegexRule(bean.getPurchaserAddrPhone());
                if (!purchaserBank.equals(bank) || !purchaserAddressPhone.equals(addressPhone)) {
                    vo.setGoing(false);
                    vo.setFlag(false);
                    if (StrUtil.isNotBlank(info.getAddressNotPassReasons())) {
                        vo.setMsg(info.getAddressNotPassReasons());
                    } else {
                        vo.setMsg("购方银行账户与购方地址电话核对不通过！");
                    }
                    vo.setPass(CodeConstant.RULE_1);
                }
            } else {
                if (StrUtil.isNotBlank(bean.getPurchaserBrank()) || StrUtil.isNotBlank(bean.getPurchaserAddrPhone())) {
                    purchaserBank = getRegexRule(bean.getPurchaserBrank());
                    purchaserAddressPhone = getRegexRule(bean.getPurchaserAddrPhone());
                    if (!purchaserBank.equals(bank) || !purchaserAddressPhone.equals(addressPhone)) {
                        vo.setGoing(false);
                        vo.setFlag(false);
                        if (StrUtil.isNotBlank(info.getAddressNotPassReasons())) {
                            vo.setMsg(info.getAddressNotPassReasons());
                        } else {
                            vo.setMsg("购方银行账户与购方地址电话核对不通过！");
                        }
                        vo.setPass(CodeConstant.RULE_1);
                    }
                }
            }
        }
        return vo;
    }

    public CheckInfoRespVO checkSubmit(String id) {
        CheckInfoRespVO vo = new CheckInfoRespVO();
        InvoiceAddressRulesEntity info = getInfo(id);
        if (info!=null&& CodeConstant.COMM_CODE_1.equals(info.getForceFlag())){
            vo.setGoing(false);
            vo.setMsg("购方地址电话银行账户规则强制校验不通过!");
        }
        return vo;
    }

    /**
     * 正则：提取中文汉字和英文字母
     *
     * @return
     */
    private String getRegexRule(String s) {
        Pattern pat = Pattern.compile(REGEX_RULE);
        Matcher mat = pat.matcher(s);
        StringBuffer sb = new StringBuffer();
        while (mat.find()) {
            sb.append(mat.group());
        }
        ;
        return sb.toString();
    }

    /**
     * 查询规则开启状态
     *
     * @param pid
     * @return 购方规则
     */
    private InvoiceAddressRulesEntity getInfo(String pid) {
        InvoiceAddressRulesEntity entity = invoiceAddressRulesService.getOne(new QueryWrapper< InvoiceAddressRulesEntity >().eq("uuid", pid));
        if (entity != null && CodeConstant.RULES_OPEN.equals(entity.getAddressIsOpen())) {
            return entity;
        }
        return null;
    }


}
